﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace Beetle.Web.Extensions
{
    /// <summary>
    /// 对Page进行脚本扩展
    /// </summary>
    /// <!-- 作者:韦腾  时间:2009-11-10 -->
    public static class ScriptExtForPage
    {
        /// <summary>
        /// 脚本警告信息
        /// </summary>
        /// <param name="page">扩展Page</param>
        /// <param name="msg">信息</param>
        public static void Alert(this Page page, string msg, params object[] args)
        {
            msg = string.Format(msg, args);
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(),
                @"                
                window.setTimeout(function(){alert(decodeURIComponent(""" + Microsoft.JScript.GlobalObject.encodeURIComponent(msg) + @"""));},0);
                ",
                 true);
        }

        /// <summary>
        /// 脚本提示信息。并自动关闭提示框。
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        /// <param name="args"></param>
        public static void ShowPromptMessage(this Page page, string msg, params object[] args)
        {
            msg = string.Format(msg, args);
            string script = @"
          
    (function(msg)
    {
        try
        {
            var Win  = window.top;

            if (typeof (Win.__promptMsgDiv) == ""undefined"")
            {
                Win.__promptMsgDiv = Win.document.createElement(""div"");
                Win.__promptMsgDiv.style.cssText = ""border: solid 5px #000000; padding: 0px 20px 0px 20px; font-family:幼圆; background-color: #EFBA4C;height: 32px; line-height: 32px; visibility: visible; position: absolute; color: White;font-size: 16px; font-weight: bold;top:0px;left:0px;"";
                Win.document.body.appendChild(Win.__promptMsgDiv);
            }

            Win.__promptMsgDiv.innerText = msg;
            Win.__promptMsgDiv.style.visibility = ""visible"";           
            Win.__promptMsgDiv.style.top = (Win.document.documentElement.clientHeight / 2 - 60) + ""px"";
            Win.__promptMsgDiv.style.left = ((Win.document.documentElement.clientWidth - Win.__promptMsgDiv.clientWidth) / 2) + ""px"";
            Win.__close__promptMsgDiv=function()
            {
                Win.__promptMsgDiv.style.visibility = ""hidden"";
                Win.__promptMsgDiv.style.top = ""0px"";
                Win.__promptMsgDiv.style.left = ""0px"";
            }

            Win.setTimeout(Win.__close__promptMsgDiv, 1500);
        }
        catch (e)
        {
            if (typeof (Win.__promptMsgDiv) != ""undefined"")
            {
                Win.__promptMsgDiv.style.visibility = ""hidden"";
                Win.__promptMsgDiv.style.top = ""0px"";
                Win.__promptMsgDiv.style.left = ""0px"";
            }

            alert(msg);
        }
    })(""" + System.Web.HttpUtility.HtmlEncode(msg) + @""");";

            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), script, true);
        }

        public static void Reload(this Page page)
        {
            page.Response.Redirect(page.Request.Url.AbsoluteUri);
        }

        /// <summary>
        /// 脚本警告信息, 并跳转页面
        /// </summary>
        /// <param name="page">扩展Page</param>
        /// <param name="msg">信息</param>
        /// <param name="url">将要跳转到的页面</param>
        public static void AlertAndRedirect(this Page page, string msg, string url)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(),
                @"                
                window.setTimeout(function(){alert(decodeURIComponent(""" + Microsoft.JScript.GlobalObject.encodeURIComponent(msg) + @""")); window.location.href=""" + url + @""";},0);
                ",
                 true);
        }


        /// <summary>
        /// 脚本警告信息,并关闭窗口
        /// </summary>
        /// <param name="page">扩展Page</param>
        /// <param name="msg">信息</param>
        public static void AlertAndClose(this Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(),
                @"                
                window.setTimeout(function(){alert(decodeURIComponent(""" + Microsoft.JScript.GlobalObject.encodeURIComponent(msg) + @"""));window.close();},0);
                ",
                 true);
        }

        /// <summary>
        /// 脚本警告信息,并关闭窗口
        /// </summary>
        /// <param name="page">扩展Page</param>
        /// <param name="windowReturnValue">窗口返回的值</param>
        /// <param name="msg">信息</param>
        public static void AlertAndClose(this Page page, string msg, string windowReturnValue)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(),
                @"                
                window.setTimeout(function(){alert(decodeURIComponent(""" + Microsoft.JScript.GlobalObject.encodeURIComponent(msg) + @"""));window.returnValue=" + windowReturnValue + @";window.close();},0);
                ",
                 true);
        }
    }
}
